﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Items = BomberManServer.Gaming.Object.Mapping.Items;
using BomberManServer.Gaming.Time;
using BomberManServer.Gaming.Object.Mapping;
using BomberManServer.Utils;

namespace BomberManServer.Gaming.Object.Bonuses
{
    public class BonusTemp : Items.Bonus
    {
        // Avatar currently owning the BonusTemp
        public Items.Avatar avatar { get; private set; }
        
        // Block spawning the BonusTemp
        public Block block { get; set; }

        // Countdown of the bonus on the avatar
        private Trigger triggerDetach;

        // Countdown before BonusTemp respawn on the block
        private Trigger triggerRepop;

        
        public BonusTemp(Game game, Type type) : base(game,type)
        {
            this.triggerDetach = new Trigger(Constants.instance.tempDuration * Constants.instance.actions_per_second,this.detach);
            this.triggerRepop = new Trigger(Constants.instance.tempRepop * Constants.instance.actions_per_second, this.repop);
            //this.block = block;
        }

        /// Gives the BonusTemp to an avatar, set his bonus stats.
        public override void attach(BomberManServer.Gaming.Object.Mapping.Items.Avatar avatar)
        {
            switch (type)
            {
                case Type.BOMB :
                    avatar.bomb_max += Constants.instance.tempPower;
                    break;
                case Type.DELAY :
                    avatar.bomb_delay += Constants.instance.tempPower;
                    break;
                case Type.RANGE :
                    avatar.deflag_length += Constants.instance.tempPower;
                    break;
                case Type.SPEED :
                    avatar.speed += Constants.instance.tempPower;
                    break;
                case Type.KICK :
                    avatar.hasKick = true;
                    break;
                default :
                    break;
            }
            this.avatar = avatar;
            avatar.game.scheduler.schedule(triggerDetach);
            avatar.game.scheduler.schedule(triggerRepop);
        }

        /// Method to detach the bonusTemp from the avatar.
        /// Reset the Avatar bonusStats.
        public void detach()
        {
            switch (type)
            {
                case Type.BOMB:
                    avatar.bomb_max -= Constants.instance.tempPower;
                    break;
                case Type.DELAY:
                    avatar.bomb_delay -= Constants.instance.tempPower;
                    break;
                case Type.RANGE:
                    avatar.deflag_length -= Constants.instance.tempPower;
                    break;
                case Type.SPEED:
                    avatar.speed -= Constants.instance.tempPower;
                    break;
                case Type.KICK:
                    avatar.hasKick = false;
                    break;
                default:
                    break;
            }
            triggerDetach.canceled = true;
        }

        /// Method called when the repop timer is expired.
        /// Spawn the bonusTemp if free or give it to the avatar on the block
        /// or reset the repop timer if a bomb is on the block.
        public void repop()
        {
            if(type == Type.KICK)
                triggerRepop.changeCountdown(Constants.instance.tempRepop * Constants.instance.actions_per_second);
            if (!block.item.free)
            {
                
                if (block.item.GetType() == avatar.GetType())
                {
                    // if avatar on the block.
                    attach((Items.Avatar)block.item);
                }
                else
                {
                    // bomb on the block.
                    avatar.game.scheduler.schedule(triggerRepop);
                }
            }
            else
            {
                //set bonusTemp to item.
                this.game.sender.onBonusSpawn(this.block.coordinates);
                this.block.item = this;
                this.triggerDetach.canceled = true;
                this.triggerRepop.canceled = true;
            }
        }

        public override Items.Item actionDeflagration(BomberManServer.Gaming.Object.Mapping.Items.Deflagration deflagration)
        {
            if (this.type == Type.KICK)
                triggerRepop.changeCountdown(Constants.instance.actions_per_second + 1);
            this.game.scheduler.schedule(triggerRepop);
            return base.actionDeflagration(deflagration);
        }
    }
}
